/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.controller;

import com.tesam.gedeon.entity.Unit;
import com.tesam.gedeon.service.UnitService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Sistema
 */
@Controller
public class UnitController {
    
    @Autowired
    SystemUserController system ;
    @Autowired
    UnitService unitService;
    
    @RequestMapping(value = "/billingunit/index")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView ("/billingunit/index");
        
        system.loadDataSystem(mv, request);
        
        return mv;
    }
    
    @RequestMapping(value = "/billingunit/view")
    public ModelAndView view(@ModelAttribute("Unit")Unit unit , HttpServletRequest request){
        ModelAndView mv = new ModelAndView ("/billingunit/view");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("unit", unitService.getUnitById(unit));
        
        return mv;
    }
    
    @RequestMapping(value = "/billingunit/update")
    public ModelAndView update(@ModelAttribute("Unit")Unit unit , HttpServletRequest request){
        ModelAndView mv = new ModelAndView ("/billingunit/update");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("unit", unitService.getUnitById(unit));
        
        return mv;
    }
    
    @RequestMapping(value = "/billingunit/new")
    public ModelAndView newUnit(@ModelAttribute("Unit")Unit unit , HttpServletRequest request){
        ModelAndView mv = new ModelAndView ("/billingunit/new");
        
        system.loadDataSystem(mv, request); 
        
        return mv;
    }
    
    @RequestMapping(value = "/billingunit/newsave")
    public ModelAndView saveUnit(@ModelAttribute("Unit")Unit unit , HttpServletRequest request){
        ModelAndView mv = new ModelAndView ("/billingunit/save");
        
        system.loadDataSystem(mv, request); 
        
        mv.addObject("id", unitService.saveUnit(unit));
        
        return mv;
    }
    
    @RequestMapping(value = "/billingunit/searchByCode")
    public ModelAndView existCode(@ModelAttribute("Unit")Unit unit , HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/billingunit/searchByCode");
        
       mv.addObject("count", unitService.getExistCode(unit)) ; 
       
        return mv;
    }
    
    @RequestMapping(value = "/billingunit/delete")
    public ModelAndView delete(@ModelAttribute("Unit")Unit unit , HttpServletRequest request){
        ModelAndView mv = new ModelAndView ("/billingunit/delete");
        
        system.loadDataSystem(mv, request); 
        
        unitService.delete(unit);
        
        return mv;
    }
    
    @RequestMapping(value = "/billingunit/saveupdate")
    public ModelAndView saveUpdate(@ModelAttribute("Unit")Unit unit , HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/billingunit/save");
        unitService.update(unit);
        
        system.loadDataSystem(mv, request);
        mv.addObject("id", unit.getId());
        
        System.out.println("saveupdate");
        return mv ;
    }
    
      @RequestMapping(value = "/billingunit/ajax" , method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxGet() {

        int quantityRecord = 0;
        try {
            List<Unit> list = unitService.getListByLimit(0, 20, "name", "asc");

            // ArrayList<Cliente> list = clienteService.getAllClientes();
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();

            if (list.size() > 0) {
                for (Unit unit : list) {
                    JSONArray arrayPut = new JSONArray(); 
                    arrayPut.put(unit.getCode()); 
                    arrayPut.put(unit.getName());  
                    array.put(arrayPut);
                    // cantidadRegistros++;
                }
            }


            quantityRecord = unitService.getCountAll();
            json.put("iTotalRecords", quantityRecord);
            json.put("iTotalDisplayRecords", quantityRecord);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.print("Error - Controller-doAjax:" + e.getMessage());
        }

        return null;
    }
     
      @RequestMapping(value = "/billingunit/ajax", method = RequestMethod.POST)
    public @ResponseBody
    String doAjaxPost(HttpServletRequest request) {

        List<Unit> listUnit = null;
        int quantityRecord = 0;

        String[] columns = {"code", "name" , "name" };

        String startLimit = request.getParameter("iDisplayStart");
        String endLimit = request.getParameter("iDisplayLength");
        String dataToSort = columns[Integer.valueOf(request
                .getParameter("iSortCol_0"))];
        String directionOfOrder = request.getParameter("sSortDir_0");
        String textSearch = request.getParameter("sSearch").toUpperCase();

        int _startLimit = 0;
        int _endLimit = 0;

        if (startLimit != null && !startLimit.equals("0")) {

            if (!startLimit.trim().isEmpty()) {
                _startLimit = Integer.valueOf(startLimit);
            }
        }

        if (endLimit != null && !endLimit.equals("0")) {
            if (!endLimit.trim().isEmpty()) {
                _endLimit = Integer.valueOf(endLimit);
            }
        }

        if (textSearch != null) {
            if (!textSearch.trim().isEmpty()) {
                textSearch = textSearch.trim();
                Unit unit = new Unit();
                unit.setName(textSearch);
                listUnit = unitService.getListBySearch(_startLimit,_endLimit,"name" , "asc" , unit);
                quantityRecord = unitService.getCountBySearch(unit);
                return unitService.getJsonDataFromArrayList(listUnit, quantityRecord);

            }
        }

        listUnit = unitService.getListByLimit(_startLimit,
                (_endLimit), dataToSort, directionOfOrder);

        quantityRecord = unitService.getCountAll();

        return unitService.getJsonDataFromArrayList(listUnit, quantityRecord);
    }

    
}
